﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
using LOGISTCBUS.Entities;

namespace LOGISTCBUS.DAO
{
    public class CostTypeDAO : BaseDAO
    {
        #region public methods
        public Boolean insert(CostType ct)
        {
            var db = new Db();
            db.Connect();
            String sql = "insert into cost_types (name, value, description) ";
            sql += "values (@name, @value, @description);";
            var parameters = new Dictionary<String, Object>();
            parameters["@name"] = ct.Name;
            parameters["@value"] = ct.Value;
            parameters["@description"] = ct.Description;
            try
            {
                var sucess = db.Execute(sql, parameters);
                db.Commit();
                db.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                db.Rollback();
                db.Disconnect();
                return false;
                throw ex;
            }
        }

        public Boolean update(CostType ct)
        {
            var db = new Db();
            db.Connect();
            String sql = "update cost_types set name = @name, value = @value, description = @description where id= @id;";
            var parameters = new Dictionary<String, Object>();
            parameters["@name"] = ct.Name;
            parameters["@value"] = ct.Value;
            parameters["@description"] = ct.Description;
            parameters["@id"] = ct.Id;
            try
            {
                var sucess = db.Execute(sql, parameters);
                db.Commit();
                db.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                db.Rollback();
                db.Disconnect();
                return false;
                throw ex;
            }
        }

        public CostType selectById(Int32 id)
        {
            var db = new Db();
            db.Connect();
            var dt = db.Select("select * from cost_types where id = " + id);
            db.Disconnect();
            if (dt == null || dt.Rows.Count <= 0)
                return null;
            DataRow r = dt.Rows[0];
            var obj = new CostType()
            {
                Id = Convert.ToInt32(r["id"]),
                Name = r["name"].ToString(),
                Value = Convert.ToDecimal(r["value"]),
                Description = r["description"].ToString(),
                Removed = Convert.ToBoolean(r["removed"])
            };
            return obj;
        }

        public List<CostType> select()
        {
            var db = new Db();
            db.Connect();
            var lst = new List<CostType>();
            var dt = db.Select("select * from cost_types where removed = 0 order by name");
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new CostType()
                {
                    Id = Convert.ToInt32(r["id"]),
                    Name = r["name"].ToString(),
                    Value = Convert.ToDecimal(r["value"]),
                    Description = r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst;
        }

        public List<CostType> select(String name, Int32 page, Int32 pageSize)
        {
            var db = new Db();
            db.Connect();
            var lst = new List<CostType>();
            var sql = "select * from cost_types where removed = 0";
            if (!String.IsNullOrEmpty(name))
                sql += " and name like '%" + name + "%'";
            sql += " order by name";
            var dt = db.Select(sql);
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new CostType()
                {
                    Id = Convert.ToInt32(r["id"]),
                    Name = r["name"].ToString(),
                    Value = Convert.ToDecimal(r["value"]),
                    Description = r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        public Int32 getCount(String name)
        {
            var count = 0;
            var db = new Db();
            db.Connect();
            var lst = new List<CostType>();
            var sql = "select count(*) as qtd from cost_types where removed = 0";
            if (!String.IsNullOrEmpty(name))
                sql += " and name like '%" + name + "%'";
            var dt = db.Select(sql);
            db.Disconnect();
            if (dt != null && dt.Rows.Count > 0)
            {
                var r = dt.Rows[0];
                count = Convert.ToInt32(r["qtd"]);
            }
            return count;
        }

        public List<CostType> select(String filter)
        {
            var db = new Db();
            db.Connect();
            var lst = new List<CostType>();
            var dt = db.Select("select * from cost_types where removed = 0 and " + getConditions(filter, "name") + " order by name");
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new CostType()
                {
                    Id = Convert.ToInt32(r["id"]),
                    Name = r["name"].ToString(),
                    Value = Convert.ToDecimal(r["value"]),
                    Description = r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst;
        }

        public Boolean delete(CostType ct)
        {
            var db = new Db();
            db.Connect();
            String sql = getSQLDeleteUpdate("cost_types", "name");
            var parameters = new Dictionary<String, Object>();
            parameters["@id"] = ct.Id;
            var sucess = db.Execute(sql, parameters);
            db.Commit();
            db.Disconnect();
            return sucess;
        }

        public Boolean delete(List<CostType> lst)
        {
            var db = new Db();
            db.Connect();
            var sucess = true;
            foreach (var ct in lst)
            {
                String sql = getSQLDeleteUpdate("cost_types", "name");
                var parameters = new Dictionary<String, Object>();
                parameters["@id"] = ct.Id;
                sucess = db.Execute(sql, parameters);
            }
            db.Commit();
            db.Disconnect();
            return sucess;
        }
        #endregion
    }
}
